package cn.hyatt.auth.entity;

import cn.hyatt.auth.enums.MenuType;
import cn.hyatt.core.entity.BaseEntity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.AssertFalse;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜单
 * @author hyatt
 */
@Getter
@Setter
@TableName(value = "system_menu")
@Schema(name = "菜单")
public class SystemMenu extends BaseEntity {


    @TableField(value = "parent_id")
    @Schema(description = "上级菜单")
    private Long parentId;

    @NotNull
    @TableField(value = "type")
    @Schema(description = "菜单类型")
    private MenuType type;

    @NotBlank
    @TableField(value = "title")
    @Schema(description = "菜单标题")
    private String title;

    @NotNull
    @TableField(value = "icon")
    @Schema(description = "菜单图标")
    private String icon;

    @NotNull
    @TableField(value = "visible")
    @Schema(description = "是否显示：true显示 false隐藏")
    private Boolean visible;

    @NotNull
    @TableField(value = "sort")
    @Schema(description = "排序")
    private Integer sort;

    @TableField(value = "path")
    @Schema(description = "路由地址")
    private String path;

    @TableField(value = "name")
    @Schema(description = "路由名称")
    private String name;

    @TableField(value = "component")
    @Schema(description = "组件路径")
    private String component;

    @TableField(value = "link")
    @Schema(description = "是否为外链：true是 false否")
    private Boolean link;

    @TableField(value = "redirect")
    @Schema(description = "重定向")
    private String redirect;

    @TableField(value = "permissions")
    @Schema(description = "权限字符串")
    private String permissions;

    @NotNull
    @TableField(value = "enabled")
    @Schema(description = "是否启用：true启用 false未启用")
    private Boolean enabled;


    @TableField(exist = false)
    @Schema(description = "子菜单")
    private List<SystemMenu> children = new ArrayList<>();

    @AssertFalse(message = "路由地址不能为空")
    public boolean isCanPathBeNull() {
        if (type.equals(MenuType.MENU)) {
            return path == null;
        }
        return false;
    }

    @AssertFalse(message = "路由名称不能为空")
    public boolean isCanNameBeNull() {
        if (type.equals(MenuType.MENU)) {
            return name == null;
        }
        return false;
    }
    @AssertFalse(message = "组件路径不能为空")
    public boolean isCanComponentBeNull() {
        if (type.equals(MenuType.MENU) || type.equals(MenuType.DIRECTORY)) {
            return component == null;
        }
        return false;
    }

    @AssertFalse(message = "链接不能为空")
    public boolean isCanLinkBeNull() {
        if (type.equals(MenuType.MENU)) {
            return link == null;
        }
        return false;
    }

    @AssertFalse(message = "重定向不能为空")
    public boolean isCanRedirectBeNull() {
        if (type.equals(MenuType.MENU) && link) {
            return redirect == null;
        }
        return false;
    }

    @AssertFalse(message = "权限字符串不能为空")
    public boolean isCanPermissionsBeNull() {
        if (type.equals(MenuType.BUTTON)) {
            return permissions == null;
        }
        return false;
    }

}
